/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.simple.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.hcht.xrlh.simple.entity.TestSimple;
import com.hcht.xrlh.simple.service.TestSimpleService;

/**
 * 测试用Controller
 * @author 测试
 * @version 2016-12-01
 */
@Controller
@RequestMapping(value = "${adminPath}/simple/testSimple")
public class TestSimpleController extends BaseController {

	@Autowired
	private TestSimpleService testSimpleService;
	
	@ModelAttribute
	public TestSimple get(@RequestParam(required=false) String id) {
		TestSimple entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = testSimpleService.get(id);
		}
		if (entity == null){
			entity = new TestSimple();
		}
		return entity;
	}
	
	@RequiresPermissions("simple:testSimple:view")
	@RequestMapping(value = {"list", ""})
	public String list(TestSimple testSimple, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TestSimple> page = testSimpleService.findPage(new Page<TestSimple>(request, response), testSimple); 
		model.addAttribute("page", page);
		return "xrlh/simple/testSimpleList";
	}

	@RequiresPermissions("simple:testSimple:view")
	@RequestMapping(value = "form")
	public String form(TestSimple testSimple, Model model) {
		model.addAttribute("testSimple", testSimple);
		return "xrlh/simple/testSimpleForm";
	}

	@RequiresPermissions("simple:testSimple:edit")
	@RequestMapping(value = "save")
	public String save(TestSimple testSimple, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, testSimple)){
			return form(testSimple, model);
		}
		testSimpleService.save(testSimple);
		addMessage(redirectAttributes, "保存测试成功");
		return "redirect:"+Global.getAdminPath()+"/simple/testSimple/?repage";
	}
	
	@RequiresPermissions("simple:testSimple:edit")
	@RequestMapping(value = "delete")
	public String delete(TestSimple testSimple, RedirectAttributes redirectAttributes) {
		testSimpleService.delete(testSimple);
		addMessage(redirectAttributes, "删除测试成功");
		return "redirect:"+Global.getAdminPath()+"/simple/testSimple/?repage";
	}

}